package chinese.admin.controllers;

import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.EnumSet;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;

import chinese.admin.constants.MemuEnum;
import chinese.admin.dao.ChnArticleCheckLogDAO;
import chinese.admin.dao.ChnArticleDAO;
import chinese.admin.domain.ChnArticle;
import chinese.admin.domain.ChnArticleCheckLog;
import chinese.constant.enums.ChnArticleStatusEnum;
import chinese.constant.enums.ChnGradeEnum;
import cj.utils.Constants.returnObj;
import cj.utils.DateUtils;
import login.admin.controllers.BaseController;
import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;

/**
 * <p>实体类</p>
 * <p>Table: ChnArticle</p>
 *
 */
@Path("chnarticle")
public class ChnArticleController extends BaseController{
	@Autowired
	private ChnArticleDAO chnarticleDao;
	@Autowired
	private ChnArticleCheckLogDAO chnarticleCheckLogDAO;
	
	@Get("show")
	public String show(Model model){
		List<Map> articleTypes = chnarticleDao.articleTypeAsEnum();
		model.add("ArticleType", articleTypes);
		EnumSet<ChnArticleStatusEnum> articleStatuses = EnumSet.allOf(ChnArticleStatusEnum.class);
		model.add("ArticleStatus", articleStatuses);
		EnumSet<ChnGradeEnum> grades = EnumSet.allOf(ChnGradeEnum.class);
		model.add("Grade", grades);
		model.add("MemuEnum", MemuEnum.chn_article);
		return "/views/chn/articleList.vm";
	}
	
	@Post("list")
	public String list(ChnArticle chnarticle) throws ParseException{
		JSONObject jo = new JSONObject();
		
		List<ChnArticle> objs = chnarticleDao.query(chnarticle);
		for(ChnArticle obj : objs){
			obj.setCreateAt(DateUtils.formatDateStr(obj.getCreateAt(), DateUtils.DATE_FULL_STR));
			obj.setUpdateAt(DateUtils.formatDateStr(obj.getUpdateAt(), DateUtils.DATE_FULL_STR));
		}
		int size = chnarticleDao.querySize(chnarticle);
		jo.put("data", objs);
		jo.put("draw", chnarticle.getDraw());
		jo.put("recordsTotal", size);
		jo.put("recordsFiltered", size);
		return "@"+jo.toString();
	}
	
	@Get("invent")
	public String invent(Model model){
		List<Map> articleTypes = chnarticleDao.articleTypeAsEnum();
		model.add("ArticleType", articleTypes);
		model.add("MemuEnum", MemuEnum.chn_article);
		return "/views/chn/articleDetail.vm";
	}
	
	@Post("create")
	public String create(ChnArticle chnarticle, HttpServletRequest request){
		JSONObject jo = new JSONObject();
		
		chnarticle.setStatus(ChnArticleStatusEnum.ALLOWED.getValue());
		
		Date current = new Date();
		String currentTime = DateUtils.getDateTime2Str(current, DateUtils.DATE_FULL_STR);
		chnarticle.setCreateAt(currentTime);
		chnarticle.setUpdateAt(currentTime);
		chnarticle.setYearForWeek(DateUtils.getYearForWeek(current));
		chnarticle.setYear(DateUtils.getDomianOfDate(current, Calendar.YEAR));
		chnarticle.setWeek(DateUtils.getDomianOfDate(current, Calendar.WEEK_OF_YEAR));
		chnarticle.setMonth(DateUtils.getDomianOfDate(current, Calendar.MONTH));
		chnarticle.setOpenId("1");
		
		String account = returnSessionObj(request).getAccount();
		chnarticle.setCreateUser(account);
		chnarticle.setUpdateUser(account);
		chnarticle.setDeleteFlag(1);
		
		chnarticleDao.create(chnarticle);
		return "@" + jo.toString();
	}
	
	@Get("edit")
	public String edit(@Param("id")int id, Model model){
		List<Map> articleTypes = chnarticleDao.articleTypeAsEnum();
		model.add("ArticleType", articleTypes);
		
		ChnArticle obj = chnarticleDao.queryById(id);
		if(obj!=null){
			model.add("Obj", obj);
		}
		model.add("MemuEnum", MemuEnum.chn_article);
		return "/views/chn/articleEdit.vm";
	}
	
	@Post("update")
	public String update(ChnArticle chnarticle){
		JSONObject jo = new JSONObject();
		
		chnarticleDao.update(chnarticle);
		return "@" + jo.toString();
	}
	
	@Get("check")
	public String check(@Param("id")int id, Model model){
		List<Map> articleTypes = chnarticleDao.articleTypeAsEnum();
		model.add("ArticleType", articleTypes);
		EnumSet<ChnArticleStatusEnum> articleStatuses = EnumSet.allOf(ChnArticleStatusEnum.class);
		model.add("ArticleStatus", articleStatuses);
		EnumSet<ChnGradeEnum> grades = EnumSet.allOf(ChnGradeEnum.class);
		model.add("Grade", grades);
		
		ChnArticle obj = chnarticleDao.articleById(id);
		if(obj!=null){
			model.add("Obj", obj);
		}
		model.add("MemuEnum", MemuEnum.chn_article);
		return "/views/chn/articleCheck.vm";
	}
	
	@Post("changeStatus")
	public String changeStatus(@Param("id")int id, @Param("status")int status, @Param("advice")String advice, HttpServletRequest request){
		JSONObject jo = new JSONObject();
		
		ChnArticle obj = chnarticleDao.queryById(id);
		if(obj != null){
			chnarticleDao.changeStatus(id, status);
			
			String account = returnSessionObj(request).getAccount();
			ChnArticleCheckLog log = new ChnArticleCheckLog();
			log.setArticleId(id);
			log.setStatus(status);
			log.setAdvice(advice);
			log.setCreateUser(account);
			log.setUpdateUser(account);
			String now = DateUtils.getNowTime();
			log.setCreateAt(now);
			log.setUpdateAt(now);
			chnarticleCheckLogDAO.create(log);
		}
		
		return "@" + jo.toString();
	}
	
	@Post("delete")
	public String delete(@Param("id")int id){
		JSONObject jo = new JSONObject();
	
		chnarticleDao.delete(id);
		return "@" + jo.toString();
	}

}